/*!
\copyright  Copyright (c) 2020-2021 Qualcomm Technologies International, Ltd.
            All Rights Reserved.
            Qualcomm Technologies International, Ltd. Confidential and Proprietary.
\file
\brief
*/

#include "unicast_server_role_advertising.h"

#include "pacs_utilities.h"
#include "le_advertising_manager.h"

#include <logging.h>

#include <panic.h>
#include <stdlib.h>
#ifdef USE_SYNERGY
#include "bap_server_lib.h"
#else
#include "bap_server.h"
#endif

static le_adv_item_handle unicast_server_registered_handle = NULL;
static le_adv_item_data_t unicast_server_advert;

static bool unicastServer_GetItemData(le_adv_item_data_t * item)
{
    DEBUG_LOG("unicastServer_GetAdvertisingDataItem unicast_server_advert.data:%d", unicast_server_advert.data);
    PanicNull(item);
    if (unicast_server_advert.data == NULL)
    {
        leAdvDataItem * unicast_bap_server_advert;

        unicast_bap_server_advert = BapServerGetAdvertisingData(BAP_SERVER_UNICAST_ROLE);
        PanicNull(unicast_bap_server_advert);

        unicast_server_advert.data = unicast_bap_server_advert->data;
        unicast_server_advert.size = unicast_bap_server_advert->size;
    }
    *item = unicast_server_advert;
    return TRUE;
}

static void unicastServer_ReleaseItemData(void)
{
    if(unicast_server_advert.data)
    {
        BapServerReleaseAdvertisingItems( BAP_SERVER_UNICAST_ROLE);
        unicast_server_advert.data = NULL;
    }
}

#ifndef INCLUDE_LEGACY_LE_ADVERTISING_MANAGER

static bool unicastServer_GetItemInfo(le_adv_item_info_t * info)
{
    PanicNull(info);
    *info = (le_adv_item_info_t){ .placement = le_adv_item_data_placement_advert,
                                    .type = le_adv_type_extended_connectable,
                                    .data_size = BapServerGetAdvertisingDataSize(BAP_SERVER_UNICAST_ROLE) };
    return TRUE;
}

le_adv_item_callback_t unicastServer_AdvertisingManagerCallback =
{
    .GetItemData = &unicastServer_GetItemData,
    .ReleaseItemData = &unicastServer_ReleaseItemData,
    .GetItemInfo = &unicastServer_GetItemInfo
};

#else

static bool unicastServer_CanAdvertise(const le_adv_data_params_t * params)
{
    bool can_advertise = FALSE;
    if(params->data_set == le_adv_data_set_extended_handset
            && params->completeness == le_adv_data_completeness_full
            && params->placement == le_adv_data_placement_advert)
    {
        can_advertise = TRUE;
    }
    return can_advertise;
}

static unsigned int unicastServer_GetNumberOfAdvertisingItems(const le_adv_data_params_t * params)
{
    if (unicastServer_CanAdvertise(params))
    {
        return BapServerGetNumberOfAdvertisingItems(BAP_SERVER_UNICAST_ROLE);
    }

    return 0;
}

static le_adv_data_item_t unicastServer_GetAdvertisingData(const le_adv_data_params_t * params, unsigned int index)
{
    /* Only one adv data item supported. If more items are needed, this function must be updated */
    PanicFalse(index == 0);
    le_adv_data_item_t item = { .size = 0, .data = NULL };
    if (unicastServer_CanAdvertise(params))
    {
        unicastServer_GetItemData(&item);
        return item;
    }

    Panic();
    return item;
}

static void unicastServer_ReleaseAdvertisingData(const le_adv_data_params_t * params)
{
    UNUSED(params);
    unicastServer_ReleaseItemData();
}

le_adv_data_callback_t unicastServer_AdvertisingManagerCallback =
{
    .GetNumberOfItems = &unicastServer_GetNumberOfAdvertisingItems,
    .GetItem = &unicastServer_GetAdvertisingData,
    .ReleaseItems = &unicastServer_ReleaseAdvertisingData
};

#endif

bool LeBapUnicastServer_SetupLeAdvertisingData(void)
{
#ifndef INCLUDE_LEGACY_LE_ADVERTISING_MANAGER
    unicast_server_registered_handle = LeAdvertisingManager_RegisterAdvertisingItemCallback(NULL, &unicastServer_AdvertisingManagerCallback);
#else
    unicast_server_registered_handle = LeAdvertisingManager_Register(NULL, &unicastServer_AdvertisingManagerCallback);
#endif
    return (unicast_server_registered_handle ? TRUE : FALSE);
}

bool LeBapUnicastServer_UpdateLeAdvertisingData(void)
{
#ifndef INCLUDE_LEGACY_LE_ADVERTISING_MANAGER
    return (unicast_server_registered_handle ? LeAdvertisingManager_UpdateAdvertisingItem(unicast_server_registered_handle) : FALSE);
#else
    return (unicast_server_registered_handle ? LeAdvertisingManager_NotifyDataChange(NULL, unicast_server_registered_handle) : FALSE);
#endif
}
